#include "stdafx.h"

#include "Engine.h"


void ClearDrawRepetition(SEngineData *ed)
{
	memset(ed->repRecord, 0, sizeof(RepetitionRecord)*MAX_GAME_LENGTH);
	memset(ed->positionStack, 0, sizeof(ZOBRIST_KEY)*MAX_GAME_LENGTH);
	ed->drawPositionCount = 0;
}

 // draw stack evalaution functions
///////////////////////////////////////////////////////////////////////

void DrawPushMove(SEngineData *ed, ZOBRIST_KEY zkeyBoard)
{
	ed->positionStack[ed->drawPositionCount++] = zkeyBoard;
}

void DrawPopMove(SEngineData *ed, ZOBRIST_KEY zkeyBoard)
{
	ed->drawPositionCount--;
}

 // checks the draw repetition
//////////////////////////////////////////////////////////////////////

bool IsDrawByRepetition(SEngineData *ed, ZOBRIST_KEY zkeyBoard)
{
	int count = ed->drawPositionCount - 2;
	int reps = 0;
	while(count >= 0)
	{
		if(ed->positionStack[count] == zkeyBoard)
			return true;
		
		count--;
	}

	return false;
}


 // General draw evaluation function
///////////////////////////////////////////////////////////////////////////////////////

bool IsDrawGeneral(SEngineData *ed, BitBoard* bb, ZOBRIST_KEY zkeyBoard)
{
	if(IsDrawByRepetition(ed, zkeyBoard))
		return true;

	return false;
}

bool IsDrawGeneral64(SEngineData *ed, BitBoard64* bb, ZOBRIST_KEY zkeyBoard)
{
	if(IsDrawByRepetition(ed, zkeyBoard))
		return true;

	return false;
}


 // wrapper templates for calling draw evaluation function
///////////////////////////////////////////////////////////////////////////////////////////

template<typename BB> bool  IsDraw(SEngineData *ed, BB* bb, ZOBRIST_KEY zkeyBoard)
{
	return false;
}

template<> bool IsDraw<BitBoard>(SEngineData *ed, BitBoard* bb, ZOBRIST_KEY zkeyBoard)
{
	return ed->IsDrawPtr32(ed, bb, zkeyBoard);
}

template<> bool IsDraw<BitBoard64>(SEngineData *ed, BitBoard64* bb, ZOBRIST_KEY zkeyBoard)
{
	return ed->IsDrawPtr64(ed, bb, zkeyBoard);
}


 // sets the draw evaluation function for given checkers type
///////////////////////////////////////////////////////////////////////////////////////////

void SetCheckersTypeDraw(SEngineData *ed, eCheckersType type)
{
	switch(type)
	{
	case CzechCheckers:
	case EnglishCheckers:
		ed->IsDrawPtr32 = IsDrawGeneral;
		break;
	case InternationalCheckers:
		ed->IsDrawPtr64 = IsDrawGeneral64;
		break;
	default:
		assert(false);
	} // switch
}
